﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IRC.IRepairClouds.DataLayer;
using System.Data.Objects;
namespace IRC.IRepairClouds.WebClient
{
    public class RoleProvider : System.Web.Security.RoleProvider
    {
        private static class CompiledQueries
        {
            public static readonly Func<ICloudRepairEntities, IEnumerable<string>> GetAllRoles =
                CompiledQuery.Compile<ICloudRepairEntities, IEnumerable<string>>(
                    (db) => from r in db.UserRole
                            select r.Name
                );

            public static readonly Func<ICloudRepairEntities, string, IEnumerable<string>> GetRolesForUser =
                CompiledQuery.Compile<ICloudRepairEntities, string, IEnumerable<string>>(
                    (db, username) => from u in db.Users
                                      where u.Login.ToLower() == username.ToLower()
                                      select u.Role.Name
                );

            public static readonly Func<ICloudRepairEntities, string, IEnumerable<string>> GetUsersInRole =
                CompiledQuery.Compile<ICloudRepairEntities, string, IEnumerable<string>>(
                    (db, roleName) => from u in db.Users
                                      where u.Role.Name.ToLower() == roleName.ToLower()
                                      select u.Login
                );

            public static readonly Func<ICloudRepairEntities, string, string, bool> IsUserInRole =
                CompiledQuery.Compile<ICloudRepairEntities, string, string, bool>(
                    (db, username, roleName) => (from u in db.Users
                                                 where u.Login.ToLower() == username.ToLower() && u.Role.Name.ToLower() == roleName.ToLower()
                                                 select u).Count() > 0
                );

            public static readonly Func<ICloudRepairEntities, string, bool> RoleExists =
                CompiledQuery.Compile<ICloudRepairEntities, string, bool>(
                    (db, roleName) => (from r in db.UserRole
                                       where r.Name.ToLower() == roleName.ToLower()
                                       select r).Count() > 0
                );
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotSupportedException();
        }

        public override string ApplicationName
        {
            get { throw new NotSupportedException(); }
            set { throw new NotSupportedException(); }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotSupportedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotSupportedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotSupportedException();
        }


        public override string[] GetAllRoles()
        {
            using (ICloudRepairEntities db = new ICloudRepairEntities())
            {
                return CompiledQueries.GetAllRoles(db).ToArray();
            }
        }


        public override string[] GetRolesForUser(string username)
        {
            using (ICloudRepairEntities db = new ICloudRepairEntities())
            {
                return CompiledQueries.GetRolesForUser(db, username).ToArray();
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            using (ICloudRepairEntities db = new ICloudRepairEntities())
            {
                return CompiledQueries.GetUsersInRole(db, roleName).ToArray();
            }
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            using (ICloudRepairEntities db = new ICloudRepairEntities())
            {
                return CompiledQueries.IsUserInRole(db, username, roleName);
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotSupportedException();
        }


        public override bool RoleExists(string roleName)
        {
            using (ICloudRepairEntities db = new ICloudRepairEntities())
            {
                return CompiledQueries.RoleExists(db, roleName);
            }
        }
    }
}